package com.ruoyi.exam.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.exam.vo.ExamBankQuestionVo;
import com.ruoyi.exam.vo.UserBankProgressAndRecordVo;
import com.ruoyi.exam.service.IExamBankQuestionService;
import com.ruoyi.exam.service.IExamUserBankProgressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.exam.vo.ExamQuestionWithSortVo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 题库题目关联Controller
 *
 * @author Tellsea
 * @date 2025-05-30
 */
@Api(tags = "题库题目关联Controller")
@RestController
@RequestMapping("/exam/bankQuestion")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamBankQuestionController extends BaseController {

    private final IExamBankQuestionService examBankQuestionService;
    private final IExamUserBankProgressService examUserBankProgressService;

    @ApiOperation("查询题库题目关联列表")
    @PreAuthorize("@ss.hasPermi('exam:bankQuestion:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamBankQuestionVo> list(ExamBankQuestionVo entity) {
        return examBankQuestionService.queryList(entity);
    }

    @ApiOperation("查询题库题目关联所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamBankQuestionVo entity) {
        return AjaxResult.success("查询成功", examBankQuestionService.queryAll(entity));
    }

    @ApiOperation("导出题库题目关联列表")
    @PreAuthorize("@ss.hasPermi('exam:bankQuestion:export')")
    @Log(title = "题库题目关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamBankQuestionVo entity) {
        List<ExamBankQuestionVo> list = examBankQuestionService.queryAll(entity);
        ExcelUtil<ExamBankQuestionVo> util = new ExcelUtil<>(ExamBankQuestionVo.class);
        util.exportExcel(response, list, "题库题目关联数据");
    }

    @ApiOperation("获取题库题目关联详细信息")
    @PreAuthorize("@ss.hasPermi('exam:bankQuestion:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", examBankQuestionService.queryById(id));
    }

    @ApiOperation("新增题库题目关联")
    @PreAuthorize("@ss.hasPermi('exam:bankQuestion:add')")
    @Log(title = "题库题目关联", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamBankQuestionVo entity) {
        return toAjax(examBankQuestionService.save(entity));
    }

    @ApiOperation("修改题库题目关联")
    @PreAuthorize("@ss.hasPermi('exam:bankQuestion:edit')")
    @Log(title = "题库题目关联", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamBankQuestionVo entity) {
        return toAjax(examBankQuestionService.updateById(entity));
    }

    @ApiOperation("删除题库题目关联")
    @PreAuthorize("@ss.hasPermi('exam:bankQuestion:remove')")
    @Log(title = "题库题目关联", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examBankQuestionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @ApiOperation("获取题库题目列表（按顺序）")
    @GetMapping("/getQuestionsByBankId/{bankId}")
    public AjaxResult getQuestionsByBankId(@PathVariable("bankId") Long bankId) {
        Long userId = getUserId();
        List<ExamQuestionWithSortVo> questions = examBankQuestionService.getQuestionsByBankId(userId,bankId);
        return AjaxResult.success("查询成功", questions);
    }

    @ApiOperation("获取题库题目id列表（按顺序）")
    @GetMapping("/getQuestionIdsByBankId/{bankId}")
    public AjaxResult getQuestionIdsByBankId(@PathVariable("bankId") Long bankId) {
        List<Long> questionIds = examBankQuestionService.getQuestionIdsByBankId(bankId);
        return AjaxResult.success("查询成功", questionIds);
    }

    @ApiOperation("获取下一题")
    @GetMapping("/getNextQuestion/{bankId}/{currentSortOrder}")
    public AjaxResult getNextQuestion(@PathVariable("bankId") Long bankId, 
                                     @PathVariable("currentSortOrder") Long currentSortOrder) {
        ExamQuestionWithSortVo nextQuestion = examBankQuestionService.getNextQuestion(bankId, currentSortOrder);
        return AjaxResult.success("查询成功", nextQuestion);
    }

    @ApiOperation("重新排序题库题目")
    @PostMapping("/reorder/{bankId}")
    public AjaxResult reorderQuestions(@PathVariable("bankId") Long bankId, 
                                      @RequestBody List<Long> questionIds) {
        examBankQuestionService.reorderQuestions(bankId, questionIds);
        return AjaxResult.success("排序成功");
    }

    @ApiOperation("获取当前用户对指定题库的答题进度和记录")
    @GetMapping("/getUserProgressAndRecords/{bankId}")
    public AjaxResult getUserProgressAndRecords(@PathVariable("bankId") Long bankId) {
        Long userId = getUserId(); // 从BaseController继承的方法获取当前用户ID
        UserBankProgressAndRecordVo result = examUserBankProgressService.getUserProgressAndRecords(userId, bankId);
        return AjaxResult.success("查询成功", result);
    }

}
